package com.sywb.chuangyebao.view.video;

import android.app.Activity;
import android.graphics.drawable.Drawable;
import android.view.View;
import android.view.ViewGroup;
import android.view.WindowManager;
import android.widget.FrameLayout;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.aliyun.vodplayer.media.IAliyunVodPlayer;
import com.sywb.chuangyebao.R;
import com.sywb.chuangyebao.bean.VideoData;
import com.sywb.chuangyebao.bean.VideoInfo;
import com.sywb.chuangyebao.contract.VideoContract;
import com.sywb.chuangyebao.library.player.AliPlayerView;
import com.sywb.chuangyebao.library.player.IVideoController;
import com.sywb.chuangyebao.library.player.listener.OnPlayerListener;
import com.sywb.chuangyebao.utils.DateTimeUtils;
import com.sywb.chuangyebao.utils.GlideUtils;
import com.sywb.chuangyebao.utils.RegexUtils;
import com.sywb.chuangyebao.utils.Utils;

import org.bining.footstone.adapter.BaseRecyclerHolder;
import org.bining.footstone.adapter.listener.OnItemClickListener;
import org.bining.footstone.adapter.listener.OnItemLongClickListener;
import org.bining.footstone.log.Logger;

public class VideoHolder extends BaseRecyclerHolder implements View.OnClickListener, IVideoController {

    public AliPlayerView llVideo;

    public FrameLayout llContainer;

    public TextView tvTitle;
    public TextView tvWatchCount;
    public LinearLayout llTitle;

    public RelativeLayout rlAvatar;
    public ImageView ivAvatar;
    public ImageView ivTutorLabel;

    public TextView tvAuthor;
    public TextView tvPraise;
    public TextView tvComment;
    public ImageView ivShare;

    private Activity activity;
    private int videoUid;

    private int postion;

    private OnVideoHolderCallback listener;

    public VideoHolder(Activity activity, android.view.View itemView, OnItemClickListener onItemClickListener, OnItemLongClickListener onItemLongClickListener) {
        super(itemView, onItemClickListener, onItemLongClickListener);
        this.activity = activity;
    }

    public void initView() {

        this.llContainer = itemView.findViewById(R.id.ll_container);
        this.llTitle = itemView.findViewById(R.id.ll_title);
        this.tvTitle = itemView.findViewById(R.id.tv_title);
        this.tvWatchCount = itemView.findViewById(R.id.tv_watch_count);
        this.rlAvatar = itemView.findViewById(R.id.rl_avatar);
        this.ivAvatar = itemView.findViewById(R.id.iv_avatar);
        this.ivTutorLabel = itemView.findViewById(R.id.iv_tutor_label);
        this.tvAuthor = itemView.findViewById(R.id.tv_author);
        this.tvPraise = itemView.findViewById(R.id.tv_praise);
        this.tvComment = itemView.findViewById(R.id.tv_comment);
        this.ivShare = itemView.findViewById(R.id.iv_share);

        ivAvatar.setOnClickListener(this);
        tvAuthor.setOnClickListener(this);
        tvPraise.setOnClickListener(this);
        tvComment.setOnClickListener(this);
        ivShare.setOnClickListener(this);

        llContainer.removeAllViews();

        ViewGroup.LayoutParams layoutParams = llContainer.getLayoutParams();
        layoutParams.width = ViewGroup.LayoutParams.MATCH_PARENT;
        layoutParams.height = (int) (org.bining.footstone.utils.ScreenUtils.getScreenWidth() * 0.56F);
        llContainer.setLayoutParams(layoutParams);

        if (llVideo != null) {
            llVideo.destroy();
        }

        llVideo = new AliPlayerView(mContext);
        llVideo.setOnPlayerListener(new OnPlayerListener() {
            @Override
            public void onInit() {
                showAvatar();
            }

            @Override
            public void onPrepared() {
                hideAvatar();
            }

            @Override
            public void onStarted() {
                hideAvatar();
                if (listener != null) {
                    listener.onStarted(postion);
                }
            }

            @Override
            public void onPaused() {
                hideAvatar();
                if (listener != null) {
                    listener.onPaused(postion);
                }
            }

            @Override
            public void onCompletion() {
                hideAvatar();
                if (listener != null) {
                    listener.onCompletion(postion);
                }
            }

            @Override
            public void onError(int causeCode, int sceneCode, String message) {
                hideAvatar();
                if (listener != null) {
                    listener.onError(postion, causeCode, sceneCode, message);
                }
            }

            @Override
            public void onShare(int platformType) {
                if (listener != null) {
                    listener.intentShare(postion);
                }
            }

            @Override
            public void onClickScreenDirection() {
                if (listener != null) {
                    listener.onClickScreenDirection(postion, llVideo.getAliyunVodPlayer().getCurrentPosition());
                }
            }

            @Override
            public void showToolsView() {
                llTitle.setVisibility(android.view.View.VISIBLE);
            }

            @Override
            public void hideToolsView() {
                llTitle.setVisibility(android.view.View.GONE);
            }

            @Override
            public void updateProgress(int curPosition, int bufferPosition, int duration) {
            }
        });

        llContainer.addView(llVideo);
    }

    public void setVideoUid(int videoUid) {
        this.videoUid = videoUid;
    }

    /**
     * 初始化值
     */
    public void setItemData(int postion, VideoData videoInfo) {
        initView();

        this.postion = postion;

        Logger.e("setItemData:" + postion);

        tvTitle.setText(videoInfo.title);
        tvWatchCount.setText(videoInfo.count.getPlayTimesTotal());

        if (videoUid == 1234567) {
            tvAuthor.setVisibility(android.view.View.GONE);
            rlAvatar.setVisibility(android.view.View.GONE);
        } else {
            tvAuthor.setVisibility(android.view.View.VISIBLE);
            rlAvatar.setVisibility(android.view.View.VISIBLE);
        }
        GlideUtils.getCircleImageToUrl(activity, videoInfo.owner.getShowAvatar(), ivAvatar, R.drawable.head_image);
        Utils.setUserRoleIcon(ivTutorLabel, videoInfo.owner.user_role);
        tvAuthor.setText(videoInfo.owner.getShowName());

        setIsThumb(videoInfo.is_thumb, videoInfo.count.thumb_up);

        if (videoInfo.count.comment > 0) {
            tvComment.setText(RegexUtils.getFormatNum(videoInfo.count.comment));
        } else {
            tvComment.setText("评论");
        }

        init(activity, IVideoController.MODE_VIDEO, videoInfo.url, videoInfo.logo, DateTimeUtils.getMediaTimeNumber(videoInfo.length));
    }

    public void setIsThumb(boolean isThumb, int thumbCount) {
        Drawable drawable;
        if (isThumb) {
            drawable = activity.getResources().getDrawable(R.drawable.praise_fill);
        } else {
            drawable = activity.getResources().getDrawable(R.drawable.praise);
        }
        drawable.setBounds(0, 0, drawable.getMinimumWidth(), drawable.getMinimumHeight());
        tvPraise.setCompoundDrawables(drawable, null, null, null);
        tvPraise.setText(thumbCount > 0 ? RegexUtils.getFormatNum(thumbCount) : "点赞");
    }

    public void showAvatar() {
        llTitle.setVisibility(android.view.View.VISIBLE);
        if (videoUid != 1234567) {
            rlAvatar.setVisibility(android.view.View.VISIBLE);
        }
    }

    public void hideAvatar() {
        llTitle.setVisibility(android.view.View.GONE);
        if (videoUid != 1234567) {
            rlAvatar.setVisibility(android.view.View.GONE);
        }
    }

    @Override
    public void onClick(android.view.View v) {
        switch (v.getId()) {
            case R.id.iv_avatar:
                if (listener != null) {
                    listener.intentUser(postion);
                }
                break;
            case R.id.tv_author:
                if (listener != null) {
                    listener.intentUser(postion);
                }
                break;
            case R.id.tv_praise:
                if (listener != null) {
                    listener.intentPraise(postion);
                }
                break;
            case R.id.tv_comment:
                if (listener != null) {
                    listener.intentComment(postion);
                }
                break;
            case R.id.iv_share:
                if (listener != null) {
                    listener.intentShare(postion);
                }
                break;
        }
    }

    /**
     * 注册监听
     */
    public void setVideoHolderCallback(OnVideoHolderCallback listener) {
        this.listener = listener;
    }

    /**
     * 解除监听
     */
    public void unRegisterVideoPlayerListener() {
        if (listener != null) {
            listener = null;
        }
    }

    public long getCurPosition() {
        return llVideo.getAliyunVodPlayer().getCurrentPosition();
    }

    @Override
    public void init(int mode) {

    }

    @Override
    public void init(Activity mActivity, int mode, String playUrl, String previewPath, String duration) {
        llVideo.init(mActivity, mode, playUrl, previewPath, duration);
    }

    @Override
    public void start() {
        if (llVideo.getPlayerState() != IAliyunVodPlayer.PlayerState.Error) {
            llVideo.start();
            activity.getWindow().addFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON);
        }
    }

    @Override
    public void start(Activity mActivity, String playUrl, String previewPath, String duration) {

    }

    @Override
    public void pause() {
        llVideo.pause();
    }

    @Override
    public void autoPause() {
        llVideo.autoPause();
    }

    @Override
    public void resume() {
        llVideo.resume();
    }

    @Override
    public void autoResume() {
        llVideo.autoResume();
    }

    @Override
    public void stop() {

    }

    @Override
    public void destroy() {
        llVideo.destroy();
        activity.getWindow().clearFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON);
    }

    @Override
    public void seekTo(int ms) {
        llVideo.seekTo(ms);
    }

    @Override
    public void setCanReplay(boolean isCanReplay) {
        llVideo.setCanReplay(isCanReplay);
    }

    @Override
    public void error(int causeCode, int sceneCode, String message) {
        llVideo.error(causeCode, sceneCode, message);
        activity.getWindow().clearFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON);
    }

    @Override
    public void showToolsView() {
        llVideo.showToolsView();
    }

    @Override
    public void showToolsView(int time) {
        llVideo.showToolsView(time);
    }

    @Override
    public void hideToolsView() {
        llVideo.hideToolsView();
    }

    @Override
    public void updateProgress(int curPosition, int bufferPosition, int duration) {
        llVideo.updateProgress(curPosition, bufferPosition, duration);
    }

    @Override
    public void changedConfiguration() {
        llVideo.changedConfiguration();
    }

    @Override
    public boolean inspectPlayer() {
        return llVideo.inspectPlayer();
    }

    @Override
    public boolean isCanPlayer() {
        return false;
    }

}
